﻿#ifndef TLVTHREADCOM_H
#define TLVTHREADCOM_H

#include <QObject>
#include <QThread>
#include "ICom.h"

class TlvThreadCom : public QObject
{
    Q_OBJECT
public:
    explicit TlvThreadCom(QObject *parent = nullptr);
    ~TlvThreadCom();

    bool isOpen() const;
    QString hwName() const;

    // 设置新的 ICom 实例（会替换旧的）
    void setWorker(ICom* worker);

    // 同步写，只在 TlvThreadCom 中提供
    QList<Tlv> syncWrite(const Tlv& tlv, uint16_t timeout = 1000);
    bool syncWrite(const Tlv& inTlv, Tlv& outTlv, uint16_t timeout=1000);

    ICom* worker();

    static TlvThreadCom* instance();

public slots:
    void open();
    void close();
    void write(const Tlv& tlv);
//    void write(const TlvTag& tag, const QByteArray& value);

signals:
    void frameDone();
    void dataSend(const Tlv&);
    void dataReceived(const Tlv&);
    void errorOccurred(const QString&);
    void opened();
    void closed();
    void statusChanged(bool);

private:
    void bindWorkerSignals(ICom* worker);
    void unbindWorkerSignals(ICom* worker);

private:
    QThread* m_thd;
    ICom* m_worker;
};

#endif // TLVTHREADCOM_H
